package pro.softzhang.algo.lc2300;

import java.util.TreeSet;

/**
 * 2336. 无限集中的最小数字
 * https://leetcode.cn/problems/smallest-number-in-infinite-set
 */
public class LC2336_SmallestNumberInInfiniteSet {
    public static void main(String[] args) {

    }

    /**
     * Your SmallestInfiniteSet object will be instantiated and called as such:
     * SmallestInfiniteSet obj = new SmallestInfiniteSet();
     * int param_1 = obj.popSmallest();
     * obj.addBack(num);
     */
    static
    class SmallestInfiniteSet {
        private int threshold;
        private TreeSet<Integer> set;

        public SmallestInfiniteSet() {
            threshold = 1;
            set = new TreeSet<>();
        }

        public int popSmallest() {
            return set.isEmpty() ? threshold++ : set.pollFirst();
        }

        public void addBack(int num) {
            if (num < threshold) {
                set.add(num);
            }
        }
    }
}
